var q = require('q');
var router = require('koa-router')({
    prefix: '/project/:id/stationconfig/:stationId'
});
var models = require('./../../../models');

router.get('/learn', function* (next) {
    var points = yield this.state.station.getTargetPrismConfigs();
    this.render('project/station.config.learn.jade', {
        project: this.state.project,
        station: this.state.station,
        points: points,
        url: this.request.url
    });
});

/** 
 * 储存点
 */
router.post('/learn', function* (next) {
    var pt = yield models.TargetPrismConfig.create(this.request.body);
    yield this.state.station.addTargetPrismConfig(pt);
    this.redirect(this.request.url);
});

/** 
 * @todo
 * 全站仪学习测量
 */
router.get('/learn/survey', function* (next) {
    if (this.state.pipeCtrl.tpsHelper) {
        yield this.state.pipeCtrl.initialTps(this.session.tpsCom, this.state.pipeCtrl.stationConfig);
        var pt = {};
        var pass = yield this.state.pipeCtrl.tpsHelper.learnSurvey(pt);
        yield this.state.pipeCtrl.tpsHelper.closePort();
        if (pass) {
            this.body = pt;
        } else {
            this.body = false;
        }
    } else {
        this.status = 412;
        this.body = false;
    }
});

/** 
 * 删除全站仪下的一个点
 */
router.get('/learn/point/:pointId/delete', function* (next) {
    var pt = yield models.TargetPrismConfig.findById(this.params.pointId);
    yield pt.destroy();
    this.redirect(`/project/${this.state.project.id}/stationconfig/${this.state.station.id}/learn`);
});

module.exports = router.routes();
